#include "snmpinc.h"
char gSysDescr[SYSNAME_LEN]=DEVICE_DESCR;
char gSysName[SYSNAME_LEN]=DEVICE_NAME;
char gSysContact[SYSNAME_LEN]="Unknow\0";
char gSysLocation[SYSNAME_LEN]="Unknow\0";
#if SNMP_WANTED==1
extern uint32_t uptime;
_UINT8 gSnmpDebug=0;
COMMUNITY_T gCommunity[MAX_TRAP_SERVER_NUM]= {{"public",COMMUNITY_RO},{"private",COMMUNITY_RW}};
_UINT8 snmp_buf[1480];

#include "mib2_system_mib.c"
#include "if_mib_mcu.c"
#include "sw1604hd_mcu.c"


SNMP_OID_TABLE_T g_mib_table[] = { 
	{ SNMP_SCALAR,{1,3,6,1,4,1,42936,4,1,0                     }, 10,  1,  7,sysinformation_scalar_handler},
	{ SNMP_SCALAR,{1,3,6,1,4,1,42936,4,2,2,0                   }, 11,  1,  5,switchconfig_scalar_handler},
	{ SNMP_SCALAR,{1,3,6,1,4,1,42936,4,2,3,0                   }, 11,  1,  5,specialkeystatus_scalar_handler},
	{ SNMP_SCALAR,{1,3,6,1,4,1,42936,4,2,4,0                   }, 11,  1,  2,signalstatus_scalar_handler},
	{ SNMP_TERMINATOR}
};
//extern unsigned char ConfigData[5];
static char debug_test_str[512];
void uart_send(char *str);
/*
int snmpDebug(const char *format, ...)
{
	va_list args;
	int len;
	memset(debug_test_str,0,sizeof(debug_test_str));
	va_start(args, format);
	len = vsnprintf((char *)debug_test_str,sizeof(version_str),format, args);
	va_end(args);
	uart_send(debug_test_str);
	return len;
}*/

_UINT32 get_sysuptime(void)
{
	//return get_sys_runtime();
	return uptime;
}
_UINT32 get_local_ip(void)
{
	_UINT32 localIP=0;
	//memcpy(&localIP,localm[NETIF_ETH].IpAdr,4);
	//localIP=ntohl(localIP);
	return localIP;
}

SNMP_OID_TABLE_T *get_mibdb_from_index(_UINT8 i)
{
	return (SNMP_OID_TABLE_T *)&g_mib_table[i];
}

_UINT16 snmp_callback(_UINT8 sock, _UINT8 *remip,
                      _UINT16 remport,_UINT8 *buf,_UINT16 len)
{
	_UINT32 addr;
	memcpy(&addr,remip,4);
	SNMP_DEBUG("snmp_callback len=%d from 0x%x:%d\r\n",len,addr,remport);
	SNMP_Process_Rcvd_Packet(buf,len,addr,remport,sock);
	SNMP_DEBUG("snmp_callback finished=%d\r\n",len);
	return 0;
}

/*  Initializes SNMP agent
 *  This function should be called before the SNMP Server application
 *  is used to set the operating parameters of it
 */
void snmp_data_init(_UINT32 sock,_UINT8 *outbuf,_UINT16 maxLen)
{
	SNMP_PKT_T *rp=(SNMP_PKT_T *)&gSnmpPkt;
	memset(rp,0,sizeof(SNMP_PKT_T));
	rp->sochandle=sock;
	rp->outbuf=(_UINT8 *)outbuf;
	rp->maxOutLen=maxLen;
	rp->state = SNMP_STATE_ENABLED;
}
_INT8 snmpd_init(void)
{
	SNMP_PKT_T *rp=(SNMP_PKT_T *)&gSnmpPkt;
	snmp_data_init(0,snmp_buf,1480);
	rp->state = SNMP_STATE_ENABLED;
	return (1);
}
void my_snmp_send(unsigned char rem_ip[4],unsigned short port,unsigned char *buf,unsigned short len);
void snmpAgentOutput(SNMP_PKT_T *rp,_UINT16 bufsize)
{
	_UINT8 addr[4];
	if(SNMP_Encode_Finish(rp,snmp_buf,bufsize) == TRUE)
	{
		memcpy(addr,&rp->remip,4);
		//snmpDebug("[S]-B send len=%d\n\r\0",bufsize);
		my_snmp_send(addr,rp->remport,snmp_buf,bufsize);
		//snmpDebug("[S]-E send len=%d\n\r\0",bufsize);
	}
}


#endif

